package com.sskj.fabi.util;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.bean.PayTypeEnum;
import com.sskj.fabi.bean.PayWayBean;
import com.sskj.fabi.http.HttpConfig;
import com.sskj.lib.util.CommonUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * Dialog 展示
 */
public class TipFabiUtil {


    private static boolean isPrice;
    private static String outMoney;
    private static String outNum;
    private PayTypeEnum payTypeYL = PayTypeEnum.YL.setChoose(false);
    private PayTypeEnum payTypeWX = PayTypeEnum.WX.setChoose(false);
    private PayTypeEnum payTypeAL = PayTypeEnum.ALIPAY.setChoose(false);

    public static MaterialDialog getPwdCheck(Context context
            , String title, String hintContent, OnTipPwdSure onTipPwdSure) {

        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.fabi_dialog_pwd, false)
                .autoDismiss(false)
                .backgroundColor(Color.TRANSPARENT)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        EditText etPwd = customView.findViewById(R.id.etPwd);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        TextView tvCancel = customView.findViewById(R.id.tvCancel);
        tvTitle.setText(title);
        etPwd.setHint(hintContent);

        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        ClickUtil.click(customView.findViewById(R.id.btSubmit), () -> {
            onTipPwdSure.onSure(etPwd.getText().toString());
        });
        ClickUtil.click(customView.findViewById(R.id.viewCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(tvCancel, () -> {
            dialog.dismiss();
        });
        return dialog;

    }

    public interface OnTipPwdSure {
        void onSure(String pwd);
    }

    public static MaterialDialog showAppeal(Context context, OnTipAppealSure onTipAppealSure) {

        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.fabi_dialog_appeal, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        View viewCancel = customView.findViewById(R.id.viewCancel);
        TextView tvCancel = customView.findViewById(R.id.tvCancel);
        TextView btSubmit = customView.findViewById(R.id.btSubmit);
        EditText etReason = customView.findViewById(R.id.etReason);

        ClickUtil.click(btSubmit, () -> {
            if (TextUtils.isEmpty(etReason.getText())) {
                ToastUtil.showShort("请输入申诉理由");
                return;
            }
            onTipAppealSure.onSure("买家未付款", etReason.getText().toString());
        });
        ClickUtil.click(viewCancel, () -> {
            dialog.dismiss();
        });
        ClickUtil.click(tvCancel, () -> {
            dialog.dismiss();
        });
        return dialog;
    }

    public static MaterialDialog showCancel(Context context, OnTipSure onTipSure) {

        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.fabi_dialog_cancel, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        View viewCancel = customView.findViewById(R.id.viewCancel);
        TextView tvCancel = customView.findViewById(R.id.tvCancel);
        TextView btSubmit = customView.findViewById(R.id.btSubmit);

        ClickUtil.click(viewCancel, () -> {
            dialog.dismiss();
        });
        ClickUtil.click(tvCancel, () -> {
            dialog.dismiss();
        });
        ClickUtil.click(btSubmit, () -> {
            onTipSure.onSure();
        });
        return dialog;
    }
//
//    public static BottomSheetDialog getPay(Context context, OnPwdGet onPwdGet) {
//
//        BottomSheetDialog codeDialog = new BottomSheetDialog(context);
//        View dialogView = LayoutInflater.from(context)
//                .inflate(R.layout.fabi_bottom_sheet_check_code, null);
//        EditText mPayPwdEditText = dialogView.findViewById(R.id.etCode);
//        ClickUtil.click(dialogView.findViewById(R.id.tvCancel), codeDialog::dismiss);
//        codeDialog.setContentView(dialogView);
//        ClickUtil.click(dialogView.findViewById(R.id.btSubmit), () -> { //提交修改
//            String pwd = mPayPwdEditText.getText().toString().trim();
//            if (TextUtils.isEmpty(pwd)) {
//                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabiTipFabiUtil33));
//                return;
//            }
//            if (!CheckUtil.isPayPwd(pwd)) {
//                ToastUtil.showShort(App.INSTANCE.getString(R.string.fabiTipFabiUtil34));
//                return;
//            }
//            onPwdGet.onSure(pwd);
//
//        });
//        return codeDialog;
//
//    }

    public static MaterialDialog showQR(Context context, String url, String content) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.fabi_dialog_qrcode, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        ImageView ivQRCode = customView.findViewById(R.id.ivQRCode);
        TextView tvContent = customView.findViewById(R.id.tvContent);
        View viewCancel = customView.findViewById(R.id.viewCancel);
        TextView tvCancel = customView.findViewById(R.id.tvCancel);
        ImageUtil.setImage(HttpConfig.BASE_IMG_URL + url, ivQRCode);
        tvContent.setText(content);
        ClickUtil.click(viewCancel, () -> {
            dialog.dismiss();
        });
        ClickUtil.click(tvCancel, () -> {
            dialog.dismiss();
        });
        return dialog;
    }

    public static MaterialDialog showPublish(Context context, boolean isBuy,
                                             String price, String num, OnPwdGet onPwdGet) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.fabi_dialog_publish, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        TextView tvPriceTitle = customView.findViewById(R.id.tvPriceTitle);
        TextView tvPrice = customView.findViewById(R.id.tvPrice);
        TextView tvNumTitle = customView.findViewById(R.id.tvNumTitle);
        TextView tvNum = customView.findViewById(R.id.tvNum);
        TextView tvMoneyTitle = customView.findViewById(R.id.tvMoneyTitle);
        TextView tvMoney = customView.findViewById(R.id.tvMoney);
        EditText etPwd = customView.findViewById(R.id.etPwd);
        Button btSubmit = customView.findViewById(R.id.btSubmit);
        View viewCancel = customView.findViewById(R.id.viewCancel);
        TextView tvCancel = customView.findViewById(R.id.tvCancel);
        tvPriceTitle.setText(isBuy ? "求购单价" : "出售单价");
        tvNumTitle.setText(isBuy ? "求购数量" : "出售数量");
        tvMoneyTitle.setText(isBuy ? "求购金额" : "出售金额");
        tvPrice.setText(price + " CNY");
        tvNum.setText(num + " USDT");
        String monetStr = CommonUtil.dealCNY(new BigDecimal(price).multiply(new BigDecimal(num)).toPlainString());
        tvMoney.setText(monetStr + " CNY");

        ClickUtil.click(viewCancel, () -> {
            dialog.dismiss();
        });
        ClickUtil.click(tvCancel, () -> {
            dialog.dismiss();
        });

        ClickUtil.click(btSubmit, () -> {

            if (TextUtils.isEmpty(etPwd.getText())) {
                ToastUtil.showShort("请输入密码");
                return;
            }
            onPwdGet.onSure(etPwd.getText().toString());
        });
        return dialog;

    }

    public static MaterialDialog showBuy(Context context, boolean isBuy, String price,
                                         String min, String max, String leftNum,
                                         String allMoney, List<PayWayBean> payWayBeans, OnBuy onBuy) {


        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.fabi_dialog_buy, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();

        ((ViewGroup) (customeView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        isPrice = true;
        outNum = "0";
        outMoney = "0";

        TextView tvTitle = customeView.findViewById(R.id.tvTitle);
        TextView tvPrice = customeView.findViewById(R.id.tvPrice);
        View viewPrice = customeView.findViewById(R.id.viewPrice);
        LinearLayout llPrice = customeView.findViewById(R.id.llPrice);
        View viewNum = customeView.findViewById(R.id.viewNum);
        LinearLayout llNum = customeView.findViewById(R.id.llNum);
        EditText etInput = customeView.findViewById(R.id.etInput);
        TextView tvUnit = customeView.findViewById(R.id.tvUnit);
        TextView tvBuyAll = customeView.findViewById(R.id.tvBuyAll);
        TextView tvLimit = customeView.findViewById(R.id.tvLimit);
        TextView tvNum = customeView.findViewById(R.id.tvNum);
        TextView tvMoney = customeView.findViewById(R.id.tvMoney);
        EditText etPwd = customeView.findViewById(R.id.etPwd);
        LinearLayout llWx = customeView.findViewById(R.id.llWx);
        LinearLayout llYl = customeView.findViewById(R.id.llYl);
        LinearLayout llAlipay = customeView.findViewById(R.id.llAlipay);
        LinearLayout llPayType = customeView.findViewById(R.id.llPayType);
        Button btSubmit = customeView.findViewById(R.id.btSubmit);

        llWx.setBackground(ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.drawable.fabi_shape_corner10_strokeff3 : R.drawable.fabi_shape_corner10_stroke05c));
        llAlipay.setBackground(ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.drawable.fabi_shape_corner10_strokeff3 : R.drawable.fabi_shape_corner10_stroke05c));
        llYl.setBackground(ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.drawable.fabi_shape_corner10_strokeff3 : R.drawable.fabi_shape_corner10_stroke05c));
        btSubmit.setBackground(ContextCompat.getDrawable(App.INSTANCE, isBuy ? R.drawable.fabi_shape_corner40_f53 : R.drawable.fabi_shape_corner40_05c));

        tvTitle.setText(isBuy ? "购买 USDT" : "出售 USDT");
        tvPrice.setText("单价￥" + price);
        tvBuyAll.setText(isBuy ? "全部购买" : "全部出售");
        ClickUtil.click(llNum, () -> {
            if (isPrice) {
                isPrice = false;
                etInput.setText("");
                viewNum.setVisibility(View.VISIBLE);
                viewPrice.setVisibility(View.INVISIBLE);
                etInput.setHint("请输入法币购买数量");
                tvUnit.setText("USDT");
            }
        });
        ClickUtil.click(llPrice, () -> {
            if (!isPrice) {
                isPrice = true;
                etInput.setText("");
                viewPrice.setVisibility(View.VISIBLE);
                viewNum.setVisibility(View.INVISIBLE);
                etInput.setHint("请输入法币购买金额");
                tvUnit.setText("CNY");
            }
        });
        PayTypeEnum payTypeYL = PayTypeEnum.YL.setChoose(false);
        PayTypeEnum payTypeWX = PayTypeEnum.WX.setChoose(false);
        PayTypeEnum payTypeAL = PayTypeEnum.ALIPAY.setChoose(false);
        if (!isBuy) {
            ClickUtil.click(llYl, () -> {
                payTypeYL.setChoose(!payTypeYL.isChoose());
                llYl.setBackground(ContextCompat.getDrawable(App.INSTANCE, payTypeYL.isChoose() ? R.drawable.fabi_shape_corner10_strokeff3 : R.drawable.fabi_shape_corner10_strokedde));
            });
            ClickUtil.click(llWx, () -> {
                payTypeWX.setChoose(!payTypeWX.isChoose());
                llWx.setBackground(ContextCompat.getDrawable(App.INSTANCE, payTypeWX.isChoose() ? R.drawable.fabi_shape_corner10_strokeff3 : R.drawable.fabi_shape_corner10_strokedde));

            });
            ClickUtil.click(llAlipay, () -> {
                payTypeAL.setChoose(!payTypeAL.isChoose());
                llAlipay.setBackground(ContextCompat.getDrawable(App.INSTANCE, payTypeAL.isChoose() ? R.drawable.fabi_shape_corner10_strokeff3 : R.drawable.fabi_shape_corner10_strokedde));
            });

            Flowable.fromIterable(payWayBeans)
                    .filter(payWayBean -> payWayBean.getPayType().equals(PayTypeEnum.WX.getType()) && !payWayBean.isDisable())
                    .subscribe(payWayBean -> llWx.setVisibility(View.VISIBLE));
            Flowable.fromIterable(payWayBeans)
                    .filter(payWayBean -> payWayBean.getPayType().equals(PayTypeEnum.ALIPAY.getType()) && !payWayBean.isDisable())
                    .subscribe(payWayBean -> llAlipay.setVisibility(View.VISIBLE));
            Flowable.fromIterable(payWayBeans)
                    .filter(payWayBean -> payWayBean.getPayType().equals(PayTypeEnum.YL.getType()) && !payWayBean.isDisable())
                    .subscribe(payWayBean -> llYl.setVisibility(View.VISIBLE));
        } else {
            llPayType.setVisibility(View.GONE);
        }

        tvLimit.setText(String.format("￥%s-￥%s", min, max));
        RxTextView.afterTextChangeEvents(etInput)
                .subscribe(textViewAfterTextChangeEvent -> {
                    String string = textViewAfterTextChangeEvent.editable().toString();
                    if (TextUtils.isEmpty(string)) {
                        string = "0";
                    }
                    if (isPrice) {
                        tvMoney.setText(String.format("交易金额 ￥ %s", outMoney = CommonUtil.dealCNY(string)));
                        String num = CommonUtil.dealCoin(new BigDecimal(string).divide(new BigDecimal(price), 8, RoundingMode.DOWN).toPlainString());
                        tvNum.setText(String.format("实际购买%sUSDT", outNum = num));
                    } else {
                        tvNum.setText(String.format("实际购买%sUSDT", outNum = CommonUtil.dealCoin(string)));
                        String money = CommonUtil.dealCNY(new BigDecimal(string).multiply(new BigDecimal(price)).toPlainString());
                        tvMoney.setText(String.format("交易金额 ￥ %s", outMoney = money));
                    }
                });
        ClickUtil.click(tvBuyAll, () -> {

            String inputNum = new BigDecimal(allMoney).doubleValue() > new BigDecimal(leftNum).doubleValue() ? leftNum : allMoney;

            if (isPrice) {

                String money = CommonUtil.dealCNY(new BigDecimal(inputNum).multiply(new BigDecimal(price)).toPlainString());
                if (new BigDecimal(money).doubleValue() > new BigDecimal(max).doubleValue()) {
                    money = max;
                }
                etInput.setText(money);
            } else {
                String string = new BigDecimal(max).divide(new BigDecimal(price), 8, BigDecimal.ROUND_DOWN).toPlainString();
                if (new BigDecimal(inputNum).doubleValue() > new BigDecimal(string).doubleValue()) {
                    inputNum = string;
                }
                etInput.setText(CommonUtil.dealCoin(inputNum));
            }

        });

        ClickUtil.click(customeView.findViewById(R.id.viewCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });

        ClickUtil.click(customeView.findViewById(R.id.btSubmit), () -> {
            if (TextUtils.isEmpty(etInput.getText()) || Double.valueOf(etInput.getText().toString()) == 0) {
                ToastUtil.showShort("请输入数量或金额");
                return;
            }
            if (TextUtils.isEmpty(etPwd.getText())) {
                ToastUtil.showShort("请输入密码");
                return;
            }
            if (!isBuy) {
                StringBuilder payTypeRecord = new StringBuilder();
                if (payTypeWX.isChoose()) {
                    payTypeRecord.append("1");
                }
                if (payTypeAL.isChoose()) {
                    if (payTypeRecord.length() > 0) {
                        payTypeRecord.append(",2");
                    } else {
                        payTypeRecord.append("2");
                    }
                }
                if (payTypeYL.isChoose()) {
                    if (payTypeRecord.length() > 0) {
                        payTypeRecord.append(",3");
                    } else {
                        payTypeRecord.append("3");
                    }
                }
                if (TextUtils.isEmpty(payTypeRecord)) {
                    ToastUtil.showShort("请选择支付方式");
                    return;
                }
                onBuy.onSure(etPwd.getText().toString(), outNum, outMoney, payTypeRecord.toString());

            } else {
                onBuy.onSure(etPwd.getText().toString(), outNum, outMoney, null);
            }
        });
        return dialog;
    }


    public interface OnPwdGet {
        void onSure(String pwd);
    }

    public interface OnBuy {
        void onSure(String payPwd, String num, String money, String payType);
    }

    public interface OnTipSure {
        void onSure();
    }

    public interface OnTipAppealSure {
        void onSure(String pos, String reason);
    }
}
